package com.yazi.mxz.common.service.impl;

import javax.annotation.Resource;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.ComSetting;
import com.yazi.mxz.common.MyUtil;
import com.yazi.mxz.common.db.dao.CompanyDao;
import com.yazi.mxz.common.db.dao.CompanyMemberDao;
import com.yazi.mxz.common.db.dao.MemberDao;
import com.yazi.mxz.common.db.dao.MemberRoleDao;
import com.yazi.mxz.common.db.dao.RoleDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.CompanyMember;
import com.yazi.mxz.common.entity.beans.CompanyMemberId;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.MemberRole;
import com.yazi.mxz.common.entity.beans.MemberRoleId;
import com.yazi.mxz.common.entity.beans.Role;
import com.yazi.mxz.common.entity.enums.RoleType;
import com.yazi.mxz.common.entity.enums.SettingId;
import com.yazi.mxz.common.service.MemberService;

@Service("memberService")
public class MemberServiceImpl extends BasicService<Member> implements MemberService {
	private static Logger log = LoggerFactory.getLogger(MemberServiceImpl.class);
	
	@Resource
	MemberDao memberDao;
	@Resource
	CompanyDao companyDao;
	@Resource
	RoleDao roleDao;
	@Resource
	CompanyMemberDao companyMemberDao;
	@Resource
	MemberRoleDao memberRoleDao;
	
	@Override
	protected Dao<Member> getDao() {
		return memberDao;
	}
	
	@Override
	public Member oneByAccountName(String name) {
		log.debug("oneByAccountName：name={}", name);
		return name == null ? null : memberDao.oneByAccountName(name);
	}

	@Override
	public Member add(Member member) {
		log.debug("add：name={}", member.getName());
		ValueFactory.validate(member);
		member.setCheckCode("" + 12345);// TODO 校验码
		member.setPin(MyUtil.makePIN());
		member.setRegisterTime(DateUtil.getTimeStamp());
		member.setMemberId(null);
		MyUtil.encodePwd(member);
		return memberDao.persist(member);		
	}
	
	@Override
	public Member add(Member member, Company company) {
		log.debug("add：memberName={},companyName", member.getName(), company.getName());
		
		// 个人信息
		ValueFactory.validate(member);
		member.setCheckCode("" + 12345);// TODO 校验码
		member.setPin(MyUtil.makePIN());
		member.setRegisterTime(DateUtil.getTimeStamp());
		member.setMemberId(null);
		MyUtil.encodePwd(member);
		member = memberDao.persist(member);
		
		// 企业信息
		ValueFactory.validate(company);
		company.setStoreRemain(ComSetting.getInteger(SettingId.COMPANY_STORE_AMOUNT));
		company.setCatRemain(ComSetting.getInteger(SettingId.COMPANY_CATEGORY_AMOUNT));
		company.setMemRemain(ComSetting.getInteger(SettingId.COMPANY_MEMBER_AMOUNT));
		company.setCodeRemain(ComSetting.getInteger(SettingId.COMPANY_CODE_AMOUNT));
		company.setCodePrefix(ComSetting.getInteger(SettingId.COMPANY_CODE_PREFIX));
		company.setAddTime(DateUtil.getTimeStamp());
		company.setCompanyId(null);
		companyDao.persist(company);
		
		// 企业管理员角色
		Role role = new Role();
		ValueFactory.validate(role);
		role.setCompany(company);
		role.setName(RoleType.FIRM.getText());
		role.setPerms("*");
		role.setType(RoleType.FIRM.ordinal());
		role.setRoleId(null);
		roleDao.persist(role);
		
		// 将用户与企业绑定
		CompanyMemberId cmId = new CompanyMemberId();
		cmId.setCompanyId(company.getCompanyId());
		cmId.setMemberId(member.getMemberId());
		CompanyMember cm = new CompanyMember();
		cm.setId(cmId);
		cm.setAddTime(DateUtil.getTimeStamp());
		companyMemberDao.persist(cm);
		
		// 将用户授予企业管理员角色
		MemberRole mr = new MemberRole();
		MemberRoleId mrId = new MemberRoleId();
		mrId.setMemberId(member.getMemberId());
		mrId.setRoleId(role.getRoleId());
		mr.setId(mrId);
		memberRoleDao.persist(mr);
		
		return member;
	}

	@Override
	public Member editByMember(Member from, Member to) {
		log.debug("modifyByMember：memberId={}",to.getMemberId());
		
		// 防止篡改以下信息
		from.setBalance(null);
		from.setCheckCode(null);
		from.setIntegral(null);
		from.setLoginTime(null);
		from.setMemberId(null);
		from.setName(null);
		from.setPassword(null);
		from.setPasswordChecker(null);
		from.setRegisterTime(null);
		from.setSeed(null);
		from.setShakeCount(null);
		from.setShakeTime(null);
		
		ValueFactory.combine(from, to);
		return memberDao.merge(to);
	}
	
}
